package com.dudu.log.ticketLog.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.dudu.bean.UserBean;
import com.dudu.log.ticketLog.service.ITicketPushService;
import com.dudu.log.ticketLog.service.impl.TicketPushServiceImpl;

import core.CoreService;

/**
 * 查询话单推送信息
 * @author jack
 *
 */
public class TicketPushAction extends CoreService implements ITicketPushAction {
	
	// 当前登陆用户
	private final String KEY_CURRENT_USER = "key_current_user";
	//状态查询service
	private ITicketPushService ticketPushService = new TicketPushServiceImpl();
	
	/**
	 * 查询话单，按分页条件查询
	 * @return
	 */
	public String queryAll() {
		log.info("TicketAction.queryAll");
		JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
        if (currentUser != null) {
        	//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        // 总数量
	        int totalAcount = this.ticketPushService.getTotalAcount(paramSql);
	        List<JSONObject> list = this.ticketPushService.queryAll(paramSql, pageSql);
	        log.info("list=="+list); 
	        jsonArray = JSONArray.fromObject(list);
	        
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", totalAcount);
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
        } else {
			object.put("flag", "100000");
		}
        return returnData(object.toString());
	}
	
    /**
     * 查询推送失败的话单
     */
	@Override
	public String queryAllFaillPush() {
		log.info("TicketAction.queryAll");
		JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
        if (currentUser != null) {
        	//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        // 总数量
	        int totalAcount = this.ticketPushService.getTotalAcountFailPush(paramSql);
	        List<JSONObject> list = this.ticketPushService.queryAllFailPush(paramSql, pageSql);
	        log.info("list=="+list); 
	        jsonArray = JSONArray.fromObject(list);
	        
	        JSONObject dataobject = new JSONObject();
	        object.put("flag", "000000");
	        object.put("desc", "success");
	        dataobject.put("totalItemNum", totalAcount);
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
        } else {
			object.put("flag", "100000");
		}
        return returnData(object.toString());
	}
	
	/**
	 * 推送失败的话单重推
	 * @param paramSql
	 * @return
	 */
	public String repushTicket(){
		log.info("TicketAction.repushTicket");
		JSONObject object = new JSONObject();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
        if (currentUser != null) {
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        // 总数量
	        int count = this.ticketPushService.repushTicket(paramSql);
	        if(count != -1){
	        	object.put("flag", "000000");
	        	object.put("count", count);
	        }else{
	        	object.put("flag", "100000");
	        }
        } else {
			object.put("flag", "100000");
		}
        return returnData(object.toString());
	}

}
